﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.Xpo;

namespace RemoteImaging.LicensePlate
{
    class SuspectLicensePlateChecker : ILicensePlateObserver
    {
        private readonly ILicensePlateCheckService _licensePlateCheckService;
        private readonly ILicensePlateEventPublisher _licensePlateEventPublisher;
        private readonly IEnumerable<INotifySuspeciousLicensePlate> _suspeciousLicensePlateObserver;

        public SuspectLicensePlateChecker(ILicensePlateCheckService licensePlateCheckService,
                                          ILicensePlateEventPublisher licensePlateEventPublisher,
                                          IEnumerable<INotifySuspeciousLicensePlate> suspeciousLicensePlateObserver)
        {
            if (licensePlateCheckService == null)
                throw new ArgumentNullException("licensePlateCheckService");
            if (licensePlateEventPublisher == null) throw new ArgumentNullException("licensePlateEventPublisher");
            if (suspeciousLicensePlateObserver == null)
                throw new ArgumentNullException("suspeciousLicensePlateObserver");

            _licensePlateCheckService = licensePlateCheckService;
            _suspeciousLicensePlateObserver = suspeciousLicensePlateObserver;

            _licensePlateEventPublisher = licensePlateEventPublisher;
            _licensePlateEventPublisher.RegisterForLicensePlateEvent(this);
        }


        public void LicensePlateCaptured(LicensePlateInfo licensePlateInfo)
        {
            _licensePlateCheckService.Check(licensePlateInfo.LicensePlateNumber,
                result =>
                {
                    if (result != null)
                    {
                        using (var session = new Session())
                        {
                            //由于对象不能跨session操作，从新从数据库里面搜索一次，因为有cache，性能影响不大
                            var lpn = session.GetObjectByKey<LicensePlateInfo>(licensePlateInfo.Oid);
                            var r = session.GetObjectByKey<ReportedCarInfo>(result.Oid);

                            var alerm = new SuspeciousCarAlermInfo(session)
                            {
                                CapturedLicenseInfo = lpn,
                                ReportedCarInfo = r
                            };

                            foreach (var l in _suspeciousLicensePlateObserver)
                            {
                                try
                                {
                                    l.Notify(alerm);
                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                                }
                            }

                            alerm.Save();
                        }

                    }
                });
        }
    }
}